import Tetris, { tetrisBlocks } from '@/composable/tetrisBlocks'
import { defineStore } from 'pinia'

export default defineStore('tetris', {
    state: () => ({
        list: [] as Tetris[],
        level: 0 as number,
        source: 0 as number,
        play: false,
        over: false,
    }),
    getters: {
        current: (state) => {
            return state.list[0]
        },
        next: (state) => {
            return state.list[state.list.length - 1]
        }
    },
    actions: {
        makeNext() {
            const i = Math.floor(Math.random() * (tetrisBlocks.length + 2))
            const r = Math.floor(Math.random() * 4)
            const block = new Tetris(i < tetrisBlocks.length ? i : 0, r)
            this.list.push(block)
        },
        footer() {
            this.list.shift()
            this.makeNext()
            return this.current
        },
        reset() {
            this.$state = {
                list: [],
                level: 0,
                source: 0,
                play: false,
                over: false,
            }
            this.makeNext()
        }
    }
})